package org.feifei.org.service.impl;

import org.feifei.basic.query.BaseQuery;
import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.basic.util.MD5Utils;
import org.feifei.basic.util.PageList;
import org.feifei.basic.util.StrUtils;
import org.feifei.org.domain.Department;
import org.feifei.org.domain.Employee;
import org.feifei.org.mapper.EmployeeMapper;
import org.feifei.org.mapper.ShopMapper;
import org.feifei.org.query.DepartmentQuery;
import org.feifei.org.service.EmployeeService;
import org.feifei.user.domain.LoginInfo;
import org.feifei.user.mapper.LoginInfoMapper;
import org.feifei.user.service.LoginInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * EmployeeService 的实现类，实现了所有方法
 */
@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private LoginInfoMapper loginInfoMapper;

    @Resource
    private ShopMapper shopMapper;

    /** 基于分页的高级查询，包含了总条数，以及查询的数据 */
    @Override
    public PageList listByPage(BaseQuery query) {
        // 查询所有的记录数量
        Long number = employeeMapper.listByPageConut(query);
        if (number <= 0) {
            return new PageList();
        }
        List<Employee> list = employeeMapper.listByPage(query);
        // 将数据封装返回
        return new PageList(number, list);
    }

    @Override
    @Transactional
    public void save(Employee employee) {
        // 给密码加密
        String salt = StrUtils.getComplexRandomString(32);
        String encodePassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(encodePassword).setSalt(salt);
        // 将employee的信息复制给LoginInfo
        LoginInfo loginInfo = employee2LoginInfo(employee);
        // 插入到loginInfo表同时返回自增长id
        loginInfoMapper.save(loginInfo);
        employee.setLogininfo_id(loginInfo.getId());
        // 插入员工
        super.save(employee);
    }


    /**
     * 如果要删除该Employee的信息，应该同时删除loginInfo中的信息,如果同时带有shopid,应该shop
     * @param id
     */
    @Transactional
    @Override
    public void remove(Long id) {
        Employee employee = super.get(id);
        // 删除loginInfo中的信息
        loginInfoMapper.remove(employee.getLogininfo_id());
        if (employee.getShop_id() != null) {
            shopMapper.remove(employee.getShop_id());
        }
        super.remove(id);
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        // 重新加密
        String md5 = StrUtils.getComplexRandomString(32);
        String encrypPassword = MD5Utils.encrypByMd5(md5 + employee.getPassword());
        employee.setPassword(encrypPassword);
        // 将employee的信息复制给LoginInfo
        LoginInfo loginInfo = employee2LoginInfo(employee);
        // 重新查询数据库，获取logininfo的id
        loginInfo.setId(super.get(employee.getId()).getLogininfo_id());
        // 修改logininfo中的信息
        loginInfoMapper.update(loginInfo);
        // 修改employee的信息
        super.update(employee);
    }
    /**
     * 复制 员工employee的信息 到 logininfo登录信息表
     * @param employee
     * @return
     */
    private LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo loginInfo = new LoginInfo();
        // 拷贝信息
        BeanUtils.copyProperties(employee, loginInfo);
        loginInfo.setType(0);
        return loginInfo;
    }
}
